package org.jeecg.modules.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.assets.entity.AssetsInfo;
import org.jeecg.modules.assets.entity.AssetsInfoWorn;
import org.jeecg.modules.assets.service.IAssetsDoubleQuotationResultService;
import org.jeecg.modules.assets.service.IAssetsInfoService;
import org.jeecg.modules.assets.service.IAssetsInfoWornService;
import org.jeecg.modules.assets.service.IAssetsPreliminaryResultsService;
import org.jeecg.modules.assets.service.impl.MdService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;

/**
 * 资产盘点自动任务
 */

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@Slf4j
public class AssetsJob {


    @Autowired
    private IAssetsPreliminaryResultsService assetsPreliminaryResultsService;
    @Autowired
    private IAssetsDoubleQuotationResultService assetsDoubleQuotationResultService;
    @Autowired
    private MdService mdService;

    @Autowired
    private IAssetsInfoService assetsInfoService;
    @Autowired
    private IAssetsInfoWornService assetsInfoWornService;


    @Scheduled(cron = "0/60 * * * * ?")

    @PostConstruct
    public void startWron() {
        log.info("预警监测任务");
            //判断装备是否到到达预警值
            QueryWrapper<AssetsInfo> qw = new QueryWrapper();
            qw.apply("assets_info.store_use_num <assets_info.warn_num and  not  EXISTS  (SELECT 1 FROM assets_info_worn WHERE assets_info.asset_name =assets_info_worn.asset_name)");
            List<AssetsInfo> list = assetsInfoService.list(qw);
            log.info("预警列表：" + list.size());
            List<AssetsInfoWorn> assetsInfoWornList = new ArrayList<>();
            list.forEach(item -> {
                AssetsInfoWorn assetsInfoWorn = new AssetsInfoWorn();
                assetsInfoWorn.setAssetName(item.getAssetName());
                assetsInfoWorn.setCategory(item.getCategory());
                assetsInfoWorn.setCategoryName(item.getCategoryName());
                assetsInfoWorn.setStoreUseNum(item.getStoreUseNum());
                assetsInfoWorn.setWarnNum(item.getWarnNum());
                assetsInfoWorn.setTotalNum(item.getTotalNum());
                assetsInfoWorn.setRemark(item.getRemark());
                assetsInfoWorn.setGroup(item.getGroup());
                assetsInfoWornList.add(assetsInfoWorn);

            });
            //组装预警列表插入
            assetsInfoWornService.saveBatch(assetsInfoWornList);
            //获取预警取消列表
        QueryWrapper<AssetsInfo> qw1 = new QueryWrapper();
        qw1.apply("assets_info.store_use_num >=assets_info.warn_num and  EXISTS  (SELECT 1 FROM assets_info_worn WHERE assets_info.asset_name =assets_info_worn.asset_name) ");
        List<AssetsInfo> list1 = assetsInfoService.list(qw1);
        log.info("预警取消列表：" + list1.size());
        list1.forEach(item -> {
            //取消预警
            QueryWrapper<AssetsInfoWorn> qw2 = new QueryWrapper();
            qw2.eq("asset_name", item.getAssetName());
            assetsInfoWornService.remove(qw2);
        });


    }

    //根据开始时间发送企微消息
    @Scheduled(cron = "0/60 * * * * ?")
    public void startAssets() {
        log.info("开始资产盘点任务");
        try {

            //    assetsPreliminaryResultsService.startAssetsJob();

        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    @Scheduled(cron = "0/60 * * * * ?")
    public void endPreliminaryAssets() {
        log.info("资产初盘结束");
        try {
            // assetsPreliminaryResultsService.endPreliminaryAssets();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Scheduled(cron = "0/60 * * * * ?")
    public void endDoubleEndAssets() {
        log.info("资产复盘结束");
        try {
            //assetsDoubleQuotationResultService.endDoubleEndAssets();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


    /**
     * 考勤异常通知
     */
    @Scheduled(cron = "0 0 13 * * 5")
    public void exceptionNotification() {
        log.info("资产复盘结束");
        try {
            //  Integer deptId = 250;
            //  mdService.endDoubleEndAssets(null);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
